﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NHibernate.Tool.hbm2ddl;
using ToDoIt2.Model.Persistent.UI;
using ToDoIt2.UI;
using ToDoIt2.Aspects;
using System.ComponentModel;
using MonitoredUndo;
using System.Diagnostics;
using ToDoIt2.ViewModel;

namespace ToDoIt2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
	[NotifyPropertyChangedAttribute]
    public partial class MainWindow : pklib.UI.Window {
		protected App App { get { return (App.Current as App); } }

		protected TabPrefs ActiveTabPrefs { get { return this.TabControl.SelectedValue as TabPrefsVM; } }

        public MainWindow() {
            InitializeComponent();
        }

		private void OnInitialized(object sender, EventArgs e) {
			#if !DEBUG
				this.Menu.Items.Remove(this.DebugMenu);
			#endif

			this.DataContext = this.App;
			this.OnNewTab(this, null);
		}

		private void OnQuitApplication(object sender, ExecutedRoutedEventArgs e) {
			this.App.Shutdown();
		}

		private void OnNewTab(object sender, ExecutedRoutedEventArgs e) {
			using (new UndoBatch(this.App.DocumentVM as ISupportsUndo, "Create Tab", false)) {
				this.App.Document.CreateTab();
			}
		}

		private void OnRemoveTab(object sender, ExecutedRoutedEventArgs e) {
			if (null != this.ActiveTabPrefs) {
				using (new UndoBatch(this.App.DocumentVM as ISupportsUndo, "Remove Tab", false)) {
					this.App.Document.RemoveTab(this.ActiveTabPrefs);
				}
			}
		}

		private void OnShowSchema(object sender, ExecutedRoutedEventArgs e) { Model.Document.ShowSchema(); }

		private void OnNewDocument(object sender, ExecutedRoutedEventArgs e) {
			this.App.Document = new Model.Document();
		}

		private void OnUndo(object sender, ExecutedRoutedEventArgs e) {
			UndoService.Current[this.App.DocumentVM].Undo();
		}

		private void OnRedo(object sender, ExecutedRoutedEventArgs e) {
			UndoService.Current[this.App.DocumentVM].Redo();
		}

		private void CanExecuteUndo(object sender, CanExecuteRoutedEventArgs e) {
			e.CanExecute = UndoService.Current[this.App.DocumentVM].CanUndo;
		}

		private void CanExecuteRedo(object sender, CanExecuteRoutedEventArgs e) {
			e.CanExecute = UndoService.Current[this.App.DocumentVM].CanRedo;
		}
    }
}
